﻿using System;
using System.Globalization;
using System.IO;


namespace InhollandAir.App_Code
{
    public class Logging
    {
        public static void WriteError(string errorMessage)
        {
            try
            {
                string path = "~/Logging/" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";

                if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
                }

                using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
                {

                    w.WriteLine("\r\nLog Entry : ");

                    w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));

                    string err = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +

                                  ". Error Message:" + errorMessage;
                    w.WriteLine(err);
                    w.WriteLine("__________________________");
                    w.Flush();
                    w.Close();
                }
            }
            catch (Exception ex)
            {
                WriteError(ex.Message);
            }
        }
        public static void WriteActivity(string activity)
        {
            try
            {
                string path = "~/Logging/" + DateTime.Today.ToString("dd-MM-yy") + ".txt";

                if (!File.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
                {
                    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close();
                }

                using (StreamWriter w = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path)))
                {

                    w.WriteLine("\r\nLog Entry : ");

                    w.WriteLine("{0}", DateTime.Now.ToString(CultureInfo.InvariantCulture));

                    string err = "Activity on: " + System.Web.HttpContext.Current.Request.Url.ToString() +

                                  ". Message:" + activity;

                    w.WriteLine(err);
                    w.WriteLine("__________________________");
                    w.Flush();
                    w.Close();
                }

            }
            catch (Exception ex)
            {
                WriteError(ex.Message);
            }
        }
    }
}